# -*- coding: utf-8 -*-
# @Time: 2025/6/29 21:54
# @Author: wzd
# @Email: 2146333089@qq.com
# @File: cleanAstruct.py
import re
import unicodedata


def clean_text(text):
    """深度文本清洗"""
    # 标准化Unicode
    text = unicodedata.normalize("NFKC", text)

    # 移除页眉页脚（示例：页码模式）
    text = re.sub(r"第\s*\d+\s*页[^\n]*\n", "", text)

    # 处理特殊空格
    text = re.sub(r"[\u3000\xa0]+", " ", text)

    # 合并断行
    text = re.sub(r"(\S)[\n\r]+(\S)", r"\1\2", text)

    # 公式处理（简单示例）
    text = re.sub(r"\$(.*?)\$", r"公式: \1", text)

    return text.strip()


def structure_document(text):
    """基于标题结构重组文档"""
    sections = []
    current_section = {"title": "", "content": []}

    for line in text.split("\n"):
        if line.startswith("#"):
            # 保存上一章节
            if current_section["title"]:
                sections.append(current_section.copy())

            # 开始新章节
            level = len(re.match(r"#+", line).group())
            current_section = {
                "level": level,
                "title": line.strip("#").strip(),
                "content": []
            }
        else:
            if line.strip():
                current_section["content"].append(line)

    if current_section["title"]:
        sections.append(current_section)

    return sections